

nicePMplot <- function(pm.object, df,
                       yupper = .1, 
                       ylower = -.05,
                       seed = 100) {
  set.seed(seed)
  PE.results95 <- PanelEstimate(sets = pm.object,
                                confidence.level = .95, number.iterations = 2000,
                                data = df)
  
  PE.results90 <- PanelEstimate(sets = pm.object, 
                                confidence.level = .90, number.iterations = 2000,
                                data = df)
  
  results <- cbind(as.data.frame(summary(PE.results95)$summary)[, c(1, 3:4)], 
                   as.data.frame(summary(PE.results90)$summary)[, 3:4])
  
  lead <- attr(pm.object, 'lead')
  
  plot <- ggplot(data = results, aes(y = estimate, x = lead)) +
    geom_errorbar(aes(ymin = `2.5%`, 
                      ymax = `97.5%`), width=.2, size = .5) +
    geom_errorbar(aes(ymin = `5%`, ymax = `95%`), 
                  width=0, size = 1.75) +
    geom_point(size = 4.5) +
    geom_hline(yintercept = 0, color = 'red', size = .5, linetype = 'dashed') +
    scale_y_continuous(limits = c(ylower, yupper),
                       breaks = seq(-0.2, .5, .1)) +
    scale_x_continuous(breaks = lead) +
    theme_cowplot() +
    background_grid(major = 'y') +
    xlab("Years Since Democratic Transition") +
    ylab("Estimated Effect of Transition on CLI")
  
  return(list(plot = plot, estimates = results))
}
